//
//  PopView.m
//  LessoDemo
//
//  Created by decade on 27/12/2017.
//  Copyright © 2017 Lesso. All rights reserved.
//

#import "PopView.h"

@implementation PopView

- (id)initWithY:(CGFloat)Y {
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        self.alpha = 0;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        UIView *subPop = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-200/2, Y, 200, 150)];
        subPop.backgroundColor = [UIColor orangeColor];
        subPop.layer.borderWidth = 1;
        subPop.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [self setupBtns:0 title:@"View Similar Product" forView:subPop action:@selector(mySimilar)];
        [self setupBtns:50 title:@"Share Product" forView:subPop action:@selector(myShare)];
        [self setupBtns:100 title:@"Close" forView:subPop action:@selector(dismiss)];
        [self addSubview:subPop];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)setupBtns:(CGFloat)Y title:(NSString *)title forView:(UIView *)view action:(SEL)action {
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, Y, 200, 50)];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
}

- (void)mySimilar {
    if (self.similarBlock) {
        self.similarBlock();
    }
    [self dismiss];
}

- (void)myShare {
    NSLog(@"myShare");
    [self dismiss];
}

- (void)dismiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
